﻿using System;
using System.Globalization;

namespace MVVMFramework.View
{
    public class DefaultValueToBoolConverter : ConverterBase
    {
        protected override object TryConvert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // null is default value
            if (value == null)
                return true;

            Type valueType = value.GetType();
            object defaultValue = GetDefaultValue(valueType);
            if (object.Equals(value, defaultValue))
                return true;

            return false;
        }

        protected override object TryConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value)
                    return GetDefaultValue(targetType);
            }

            throw new ConverterException("Unexpected conversion type");
        }

        
    }
}
